#include <QtGui>
#include <QtOpenGL>
#include "gl/GLU.h"
#include <math.h>

#include "glwidget.h"
#include "meshloader.h"

Mesh ruins;

 GLWidget::GLWidget(QWidget *parent)
     : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
 {
    xRot=0;
    yRot=0;
    zRot=0;
    zoom = 10;
 }

 GLWidget::~GLWidget()
 {
 }

 QSize GLWidget::minimumSizeHint() const
 {
     return QSize(640, 480);
 }

 QSize GLWidget::sizeHint() const
 {
     return QSize(640, 480);
 }

 void GLWidget::initializeGL()
 {
     glClearColor(0, 0, 0, 1);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_CULL_FACE);
     glShadeModel(GL_SMOOTH);
     glEnable(GL_LIGHTING);
     glEnable(GL_LIGHT0);

     this->setFocusPolicy(Qt::StrongFocus);

     ruins.ReadObjFile("Meshes/TestScene.obj");

     timerOn();
 }

 void GLWidget::paintGL()
 {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();
     glTranslatef(0.0, 0.0, -zoom);
     glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
     glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
     glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
     ruins.draw("Cube1");
     ruins.draw("Cube0");
     ruins.draw("Scene");
 }

 void GLWidget::resizeGL(int width, int height)
 {
     glViewport(0, 0, width, height);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective(60.0f, (float)width/(float)height, 0.1, 1000.0);
     glMatrixMode(GL_MODELVIEW);
 }

 void GLWidget::mousePressEvent(QMouseEvent *event)
 {
     lastPos = event->pos();
 }

 void GLWidget::mouseMoveEvent(QMouseEvent *event)
 {
     int dx = event->x() - lastPos.x();
     int dy = event->y() - lastPos.y();

     if (event->buttons() & Qt::LeftButton) {
         setXRotation(xRot + 8 * dy);
         setYRotation(yRot + 8 * dx);
     } else if (event->buttons() & Qt::RightButton) {
         setXRotation(xRot + 8 * dy);
         setZRotation(zRot + 8 * dx);
     }
     lastPos = event->pos();
 }

 void GLWidget::wheelEvent(QWheelEvent *event)
{
    if(event->delta() < 0)
    {
        zoom += 10;
    }
    else if(event->delta() > 0)
    {
        zoom -= 10;
    }
}

 static void qNormalizeAngle(int &angle)
 {
     while (angle < 0)
         angle += 360 * 16;
     while (angle > 360 * 16)
         angle -= 360 * 16;
 }

 void GLWidget::setXRotation(int angle)
 {
     qNormalizeAngle(angle);
     if (angle != xRot) {
         xRot = angle;
         emit xRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::setYRotation(int angle)
 {
     qNormalizeAngle(angle);
     if (angle != yRot) {
         yRot = angle;
         emit yRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::setZRotation(int angle)
 {
     qNormalizeAngle(angle);
     if (angle != zRot) {
         zRot = angle;
         emit zRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::keyPressEvent(QKeyEvent* pe)
 {
     switch (pe->key())
     {
     }
}

 void GLWidget::timerOn()
 {
     timer = new QTimer(this);
     connect(timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()));
     timer->start(20);
 }

 void GLWidget::timeOut()
 {
     updateGL();
 }

 void GLWidget::timeOutSlot()
 {
     timeOut();
 }


